package com.thinkit.bigdata.web.controller.system;

import com.thinkit.bigdata.core.util.StringUtil;
import com.thinkit.bigdata.web.model.config.SysArea;
import com.thinkit.bigdata.web.service.sec.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面下拉列表查询创建人接口
 * Created by lihongli on 2019/1/10
 */
@RestController
@RequestMapping("searchUser")
public class CreateUserSearchController {

    @Autowired
    private UserService userService;

    // 标签管理页面对应table名称
    private static final String TAG_TABLE_NAME = "bus_tag";

    // 模型管理页面对应table名称
    private static final String MODEL_TABLE_NAME = "bus_model";

    // 模型测验页面对应table名称
    private static final String MODEL_TEST_TABLE_NAME = "bus_work_test";

    // 专项测验(workType = 1)、自动质检任务(workType = 0)页面对应table名称
    private static final String QUALITY_TEST_TABLE_NAME = "bus_work";

    private Integer getAreaId() {
        // 判断当前登录用户所属区域
        SysArea sysArea = (SysArea) SecurityUtils.getSubject().getSession().getAttribute("areaInfo");
        if (sysArea != null)
            return sysArea.getId();
        return null;
    }

    /**
     * 查询标签管理页面的创建人
     *
     * @return
     */
    @RequestMapping(value = "/searchTagUser", method = RequestMethod.GET)
    public List<Map<String, Object>> searchTagUser() {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", TAG_TABLE_NAME);
        params.put("areaId", getAreaId());
        return userService.selectCreateUser(params);
    }

    /**
     * 查询模型管理页面的创建人
     *
     * @return
     */
    @RequestMapping(value = "/searchModelUser", method = RequestMethod.GET)
    public List<Map<String, Object>> searchModelUser(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", MODEL_TABLE_NAME);
        // 判断是否传入areaId参数
        String areaId = request.getParameter("areaId");
        if(StringUtil.isNotEmpty(areaId) && areaId.matches("\\d*")){
            params.put("areaId", Integer.valueOf(areaId));
        }else{
            params.put("areaId", getAreaId());
        }
        return userService.selectCreateUser(params);
    }

    /**
     * 查询模型测验页面的创建人
     *
     * @return
     */
    @RequestMapping(value = "/searchModelTestUser", method = RequestMethod.GET)
    public List<Map<String, Object>> searchModelTestUser() {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", MODEL_TEST_TABLE_NAME);
        params.put("areaId", getAreaId());
        return userService.selectCreateUser(params);
    }

    /**
     * 查询专项质检页面的创建人
     *
     * @return
     */
    @RequestMapping(value = "/searchPreQualityUser", method = RequestMethod.GET)
    public List<Map<String, Object>> tagSearch() {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", QUALITY_TEST_TABLE_NAME);
        params.put("areaId", getAreaId());
        params.put("workType", 1);
        return userService.selectCreateUser(params);
    }

    /**
     * 查询自动质检任务页面的创建人
     *
     * @return
     */
    @RequestMapping(value = "/searchAutoTestUser", method = RequestMethod.GET)
    public List<Map<String, Object>> searchAutoTestUser() {
        Map<String, Object> params = new HashMap<>();
        params.put("tableName", QUALITY_TEST_TABLE_NAME);
        params.put("areaId", getAreaId());
        params.put("workType", 0);
        return userService.selectCreateUser(params);
    }
}
